package com.dmpp.captcha;

import cn.hutool.captcha.*;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.RandomUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 北橙
 * @Create 2022/6/13
 * @Description 随机验证码
 */
public class RandomCaptcha extends AbstractCaptcha {

    private AbstractCaptcha captcha;

    private static final List<AbstractCaptcha> CAPTCHA_LIST = new ArrayList<>();

    public RandomCaptcha(int width, int height, int codeCount, int interfereCount) {
        super(width, height, codeCount, interfereCount);
        init(codeCount);
    }

    public void init(int codeCount) {
        CAPTCHA_LIST.add(new LineCaptcha(width, height, codeCount, 15));
        CAPTCHA_LIST.add(new CircleCaptcha(width, height, codeCount,15));
        CAPTCHA_LIST.add(new ShearCaptcha(width, height, codeCount,3));
        CAPTCHA_LIST.add(new GifCaptcha(width, height, codeCount));
    }

    @Override
    public void createCode() {
        int index = RandomUtil.randomInt(0, 4);
        captcha = CAPTCHA_LIST.get(index);
        captcha.createCode();
    }

    @Override
    public String getCode() {
        return captcha.getCode();
    }

    @Override
    public boolean verify(String userInputCode) {
        return captcha.verify(userInputCode);
    }

    @Override
    public void write(String path) throws IORuntimeException {
        captcha.write(path);
    }

    @Override
    public void write(File file) throws IORuntimeException {
        captcha.write(file);
    }

    @Override
    public void write(OutputStream out) {
        captcha.write(out);
    }

    @Override
    public byte[] getImageBytes() {
        return captcha.getImageBytes();
    }

    @Override
    public BufferedImage getImage() {
        return captcha.getImage();
    }

    @Override
    public String getImageBase64() {
        return captcha.getImageBase64();
    }

    @Override
    public String getImageBase64Data() {
        return captcha.getImageBase64Data();
    }

    @Override
    public void setFont(Font font) {
        captcha.setFont(font);
    }

    @Override
    public CodeGenerator getGenerator() {
        return captcha.getGenerator();
    }

    @Override
    public void setGenerator(CodeGenerator generator) {
        captcha.setGenerator(generator);
    }

    @Override
    public void setBackground(Color background) {
        captcha.setBackground(background);
    }

    @Override
    public void setTextAlpha(float textAlpha) {
        captcha.setTextAlpha(textAlpha);
    }

    @Override
    protected Image createImage(String code) {
        if (captcha instanceof LineCaptcha) {
            return ((LineCaptcha) captcha).createImage(code);
        }
        if (captcha instanceof CircleCaptcha) {
            return ((CircleCaptcha) captcha).createImage(code);
        }
        if (captcha instanceof ShearCaptcha) {
            return ((ShearCaptcha) captcha).createImage(code);
        }
        return null;
    }
}
